

Ext.onReady(function(){
//	Ext.QuickTips.init();

var person_record=Ext.data.Record.create([
	{name:'jobNumber',mapping:'jobNumber',type:'string'},
	{name:'name',mapping:'name',type:'string'},
	{name:'sex',mapping:'sex',type:'string'},
	{name:'birthday',mapping:'birthday',type:'date',dateFormat:'Y-m-dTH:i:s'},
	{name:'professionalTitle',mapping:'professionalTitle',type:'string'},
	{name:'post',mapping:'post',type:'string'},
	{name:'educationLevel',mapping:'educationLevel',type:'string'},
	{name:'schoolRecord',mapping:'schoolRecord',type:'string'},
	{name:'majorField',mapping:'majorField',type:'string'},
	{name:'position',mapping:'position',type:'string'},
	{name:'state',mapping:'state',type:'string'},
	{name:'telephone',mapping:'telephone',type:'string'},
	{name:'email',mapping:'email',type:'string'},
]);

var person_reader=new Ext.data.JsonReader({
						root:'personList',
						totalProperty:'total'
					},person_record);
					
var person_store=new Ext.data.Store({
//	url:'getBelongedDepartPersonAction.action',
	proxy:new Ext.data.HttpProxy({
		url:'getDepartPersonAction.action',
		method:'post'
	}),
	reader:person_reader
}); 

/*加载数据*/
person_store.load({params:{start:0,limit:5}});


/*人员状态数据*/
var person_state_data=[[
	'在职','在职'
],[
	'离职','离职'
],[
	'退休','退休'
]];
/*对应人员状态的store*/
var person_state_store=new Ext.data.SimpleStore({
	fields:['value','text'],
	data:person_state_data
});

/*人员性别数据*/
var person_sex_data=[[
	'男','男'
],[
	'女','女'
]];
/*对应性别的store*/
var person_sex_store=new Ext.data.SimpleStore({
	fields:['value','text'],
	data:person_sex_data
});


var person_sm=new Ext.grid.CheckboxSelectionModel();

var person_cm=new Ext.grid.ColumnModel([new Ext.grid.RowNumberer({header:'全选',width:35,sortable:true}),person_sm,
	{
		header:'工号',dataIndex:'jobNumber',fited:true,resizable :false,hideable:false,width:50
	},{
		header:'姓名',dataIndex:'name',fited:true,resizable :false,hideable:false,width:60 
	},{
		header:'性别',dataIndex:'sex',fited:true,resizable :false,hideable:false,width:30 
	},{
		header:'出生年月',dataIndex:'birthday',fited:true,resizable :false,hideable:false,type:'date',renderer:Ext.util.Format.dateRenderer('Y年m月d日'),width:100
	},{
		header:'职称',dataIndex:'professionalTitle',fited:true,resizable :false,hideable:false,width:50
	},{
		header:'职务',dataIndex:'post',fited:true,resizable :false,hideable:false,width:50
	},{
		header:'学位',dataIndex:'educationLevel',fited:true,resizable :false,hideable:false,width:50
	},{
		header:'学历',dataIndex:'schoolRecord',fited:true,resizable :false,hideable:false,width:50
	},{
		header:'专业方向',dataIndex:'majorField',fited:true,resizable :false,hideable:false
	},{
		header:'所在岗位',dataIndex:'position',fited:true,resizable :false,hideable:false
	},{
		header:'联系电话',dataIndex:'telephone',fited:true,resizable :false,hideable:false
	},{
		header:'电子邮件',dataIndex:'email',fited:true,resizable :false,hideable:false
	},{
		header:'人员状态',dataIndex:'state',fited:true,resizable :false,hideable:false
	}
])

/*添加人员按钮*/
var add_person_button=new Ext.Button({
	text:'添加',
	handler:function(){
			add_person_window.show();
	}
});

/*添加人员表单*/
var add_person_form=new Ext.form.FormPanel({
	frame:true,
	autoHeight:true,
	buttonAlign:'center',
	labelAlign:'right',
	url:'addPersonByDepartAction.action',
	labelWidth:60,
	defaultType:'textfield',
	items:[{
		name:'name',
		fieldLabel:'姓名'
	},{
		name:'jobNumber',
		fieldLabel:'工号'
	},{
		name:'password',
		fieldLabel:'初始密码',
		inputType:'password'
	},{
		name:'sex',
		xtype:'combo',
		fieldLabel:'性别',
		width:60,
		store:person_sex_store,
		displayField:'text',
		valueField:'value',
		mode:'local',
		triggerAction:'all',
		emptyText:'男'
	},{
		name:'birthday',
		xtype:'datefield',
		width:150,
		format:'Y-m-d',
		fieldLabel:'出生日期'
	},{
		name:'professionalTitle',
		fieldLabel:'职称',
		allowBlank:true
	},{
		name:'post',
		fieldLabel:'职务',
		allowBlank:true
	},{
		name:'educationLevel',
		fieldLabel:'学位',
		allowBlank:true
	},{
		name:'schoolRecord',
		fieldLabel:'学历',
		allowBlank:true
	},{
		name:'majorField',
		fieldLabel:'专业方向',
		allowBlank:true
	},{
		name:'position',
		fieldLabel:'所在岗位',
		allowBlank:true
	},{
		name:'telephone',
		fieldLabel:'联系电话',
		allowBlank:true
	},{
		name:'email',
		fieldLabel:'电子邮件',
		allowBlank:true
//		vtype:'email',
//		vtypeText:'不是合法的E-mail地址'
	},{
		name:'state',
		xtype:'combo',
		fieldLabel:'人员状态',
		store:person_state_store,
		displayField:'text',
		valueField:'value',
		mode:'local',
		triggerAction:'all',
		emptyText:'在职'
	}],
	buttons:[{
		text:'提交',
		type:'submit',
		handler:function(button){
			
			var btn_form=add_person_form.getForm();

				button.disable();
				
//				var jobNumber=btn_form.findField('id');
				
				btn_form.submit({
					waitMsg:'数据提交中,请稍后......',
					waitTitle:'提交数据',
					method:'POST',
					success:function(form,action){
						person_grid.getStore().reload();
						person_grid.view.refresh();
						Ext.Msg.alert("提示信息",'人员添加成功');
						button.enable();
						add_person_form.getForm().reset();
					},
					failure:function(form,action){
						Ext.Msg.alert("提示信息","该人员已经存在")
						button.enable();
					}
				});
			}
		
	},{
		text:'重置',
		type:'reset',
		handler:function(){
			add_person_form.getForm().reset();
		}
	},{
		text:'取消',
		handler:function(){
			add_person_form.getForm().reset();
			add_person_window.hide();
		}
	}]
});


/*添加新人员窗口*/
var add_person_window=new Ext.Window({
	title:'添加新人员',
	closeAction:'destroy',
	draggable:false,
	modal:true,
	resizable:false,
	shadow:true,
	width:400,
	plain:true,
	items:[add_person_form]
});
add_person_window.setPosition(533,100);


/*删除人员按钮*/

Ext.Msg.buttonText.ok="确认";

var delete_person_button=new Ext.Button({
	
	text:'删除',
	handler:function(){
		
		var selected_record=person_grid.getSelectionModel().getSelections();
		
		if(selected_record.length==0){
			Ext.Msg.show({
				title:'提示信息',
				msg:'无任何选项，请选择!',
				buttons:{ok:'返回'}
			});
		}
		else{
			Ext.Msg.show({
				title:'提示信息',
				msg:'确认删除已选项 ?',
				buttons:{ok:'确认',no:'取消'},
				fn:function(button,text){
					if(button=="ok"){
						var jobNumberList=new Array(selected_record.length);
						
						for(var i=0;i<selected_record.length;i++){
							jobNumberList[i]=selected_record[i].get('jobNumber');
						}
						Ext.Ajax.request({
							url:"deletePersonAction.action",
							params:{jobNumberList:jobNumberList},
							success:function(){
								Ext.Msg.show({
									title:'提示信息',
									msg:'成功删除 ！',
									width:200,
									icon: Ext.Msg.INFO,
									buttons:Ext.Msg.OK
								});
								for(var i=0;i<selected_record.length;i++){
									person_grid.getStore().remove(selected_record[i])
								}
								person_grid.getStore().reload();
								person_grid.view.refresh();
							},
							failure:function(){
								Ext.Msg.alert("提示信息",'删除失败！');
							}
						});
					}
				}
			});
		}		
	}
});


/*查看人员信息按钮*/
var view_person_button=new Ext.Button({
	text:'查看',
	handler:function(){
		
		var person_records=person_grid.getSelectionModel().getSelections();
		
		if(person_records.length==0){
			Ext.Msg.show({
				title:'提示信息',
				msg:'没有选中人员,请选择 ！',
				buttons:Ext.Msg.OK
			});
		}
		if(person_records.length>1){
			Ext.Msg.show({
				title:'提示信息',
				msg:'只能选择一个人员查看 ！',
				buttons:Ext.Msg.OK
			});
		}
		alert(person_records[0].get('name'));
		view_person_form.getForm().loadRecord(person_records[0]);
		
		view_person_window.show();
	}
});

/*查看人员信息表单*/
var view_person_form= new Ext.form.FormPanel({
	
	frame:true,
	autoHeight:true,
	labelAlign:'right',
	buttonAlign:'center',
	labelWidth:60,
	defaultType:'textfield',
	items:[{
			name:'name',
			fieldLabel:'姓名'
	},{
			name:'jobNumber',
			fieldLabel:'工号'
	},{
			name:'sex',
			xtype:'combo',
			fieldLabel:'性别',
			store:person_sex_store,
			displayField:'text',
			valueField:'value',
			mode:'local',
			width:60,
			triggerAction:'all',
			emptyText:'男'
	},{
			name:'birthday',
			fieldLabel:'出生日期',
			xtype:'datefield',
			format:'Y年m月d日',
			width:150
//			disabled:true
	},{
			name:'professionalTitle',
			fieldLabel:'职称'
	},{
			name:'post',
			fieldLabel:'职务'
	},{
			name:'educationLevel',
			fieldLabel:'学位'
	},{
			name:'schoolRecord',
			fieldLabel:'学历'
	},{
			name:'majorField',
			fieldLabel:'专业方向'
	},{
			name:'position',
			fieldLabel:'所在岗位'
	},{
			name:'telephone',
			fieldLabel:'联系电话'
	},{
			name:'email',
			fieldLabel:'电子邮件'
	},{
			name:'state',
			xtype:'combo',
			hiddenName:'state',
			fieldLabel:'人员状态',
			store:person_state_store,
			displayField:'text',
			valueField:'value',
			mode:'local',
			triggerAction:'all',
			emptyText:'请选择'
	}],
	buttons:[{
		text:'确认',
		id:'confirm_button',
		handler:function(){
			view_person_form.getForm().reset();
			view_person_window.hide();
		}
	}]
});

/*查看人员信息窗口*/
var view_person_window=new Ext.Window({
		title:'人员信息',
		closeAction:'hide',
		constrainHeader:true,
		draggable:false,
		modal:true,
		resizable:false,
		shadow:true,
		width:400,
		plain:true,
		items:[view_person_form]
	});
view_person_window.setPosition(500,100);


/*编辑人员按钮*/
var edit_person_button=new Ext.Button({
	text:'编辑',
	handler:function(){
		
		var person_records=person_grid.getSelectionModel().getSelections();
		
		if(person_records.length==0){
			Ext.Msg.show({
				title:'提示信息',
				msg:'没有选中人员,请选择 ！',
				buttons:Ext.Msg.OK
			});
		}
		else if(person_records.length>1){
			Ext.Msg.show({
				title:'提示信息',
				msg:'只能选择一个人员编辑 ！',
				buttons:Ext.Msg.OK
			});
		}else{	
				edit_person_form.getForm().loadRecord(person_records[0]);
				edit_person_window.show();
		}
	}
});

/*编辑人员信息表单*/

var edit_person_form= new Ext.form.FormPanel({
	
	frame:true,
	autoHeight:true,
	labelAlign:'right',
	buttonAlign:'center',
	labelWidth:60,
	defaultType:'textfield',
	items:[{
			name:'name',
			fieldLabel:'姓名'
	},{
			name:'jobNumber',
			fieldLabel:'工号'
	},{
			name:'sex',
			xtype:'combo',
			fieldLabel:'性别',
			store:person_sex_store,
			displayField:'text',
			valueField:'value',
			mode:'local',
			width:60,
			triggerAction:'all',
			emptyText:'男'
	},{
			name:'birthday',
			fieldLabel:'出生日期',
			xtype:'datefield',
			format:'Y年m月d日',
			width:150
//			disabled:true
	},{
			name:'professionalTitle',
			fieldLabel:'职称'
	},{
			name:'post',
			fieldLabel:'职务'
	},{
			name:'educationLevel',
			fieldLabel:'学位'
	},{
			name:'schoolRecord',
			fieldLabel:'学历'
	},{
			name:'majorField',
			fieldLabel:'专业方向'
	},{
			name:'position',
			fieldLabel:'所在岗位'
	},{
			name:'telephone',
			fieldLabel:'联系电话'
	},{
			name:'email',
			fieldLabel:'电子邮件'
	},{
			name:'state',
			xtype:'combo',
			hiddenName:'state',
			fieldLabel:'人员状态',
			store:person_state_store,
			displayField:'text',
			valueField:'value',
			mode:'local',
			triggerAction:'all',
			emptyText:'请选择'
	}],
	buttons:[{
		text:'提交',
		id:'confirm_button',
		handler:function(){
			
		}
	},{
		text:'重置',
		id:'confirm_button',
		handler:function(){
			edit_person_form.getForm().reset();
		}
	},{
		text:'取消',
		id:'confirm_button',
		handler:function(){
			edit_person_form.getForm().reset();
			edit_person_window.hide();
		}
	}]
});


/*编辑人员信息窗口*/
var edit_person_window=new Ext.Window({
		title:'人员信息',
		closeAction:'hide',
		constrainHeader:true,
		draggable:false,
		modal:true,
		resizable:false,
		shadow:true,
		width:400,
		plain:true,
		items:[edit_person_form]
	});
edit_person_window.setPosition(500,100);



/*顶部功能工具条*/
var top_function_toolbar=new Ext.Toolbar();

/*底部分页条*/	
var bottom_paging_bar=new Ext.PagingToolbar({
	pageSize:5,
	store:person_store,
	emptyMsg:'没有数据需要显示',
	displayInfo:true
});

/*顶部分页条*/
var top_paging_bar=new Ext.PagingToolbar({
	pageSize:5,
	store:person_store,
	emptyMsg:'没有数据需要显示',
	displayInfo:true
});

/*在工具条上加入功能按钮*/
	for(var i=0;i<20;i++){
		top_function_toolbar.addSpacer();
	}
	
	top_function_toolbar.addText('操作选项');
	for(var i=0;i<20;i++){
		top_function_toolbar.addSpacer();
	}
	top_function_toolbar.addSeparator();
	
	for(var i=0;i<20;i++){
		top_function_toolbar.addSpacer();
	}
	
	top_function_toolbar.addButton([add_person_button]);
	for(var i=0;i<20;i++){
		top_function_toolbar.addSpacer();
	}
	top_function_toolbar.addSeparator();
	for(var i=0;i<20;i++){
		top_function_toolbar.addSpacer();
	}
	
	top_function_toolbar.addButton([delete_person_button]);
	for(var i=0;i<20;i++){
		top_function_toolbar.addSpacer();
	}
	top_function_toolbar.addSeparator();
	for(var i=0;i<20;i++){
		top_function_toolbar.addSpacer();
	}
	
	top_function_toolbar.addButton(view_person_button);
	for(var i=0;i<20;i++){
		top_function_toolbar.addSpacer();
	}
	top_function_toolbar.addSeparator();
	for(var i=0;i<20;i++){
		top_function_toolbar.addSpacer();
	}
	top_function_toolbar.addButton(edit_person_button);
	for(var i=0;i<20;i++){
		top_function_toolbar.addSpacer();
	}
	top_function_toolbar.addSeparator();
	


/*人员表格*/
var person_grid=new Ext.grid.GridPanel({
	title:'人员信息管理列表',
	store:person_store,
	cm:person_cm,
	sm:person_sm,
	autoWidth:true,
	width:1200,
	height:600,
	loadMask:{msg : '数据加载中...'},
	viewConfig:{forceFit:true},
	frame:true,
	trackMouseOver:false,
	enableColumnMove : false,
	enableColumnHide:false,
	bbar:bottom_paging_bar,
	items:[top_function_toolbar,top_paging_bar],
	renderTo:'grid'
});


});






